package com.sqpoll.android.poll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sqpoll.android.R;
import com.sqpoll.android.util.AndroidUtil;

/**
 * Adapter for answer choice list.
 * 
 */
@SuppressLint("UseSparseArrays")
public class AnswerChoiceListAdapter extends BaseAdapter {
	
	public static final String TAG = "AnswerChoiceListAdapter";
	public static final int MODIFY_MODE = 1;
	public static final int READ_MODE = 2;
	
	private int mViewMode = MODIFY_MODE;
	private LayoutInflater mInflater;
	private Context mContext;
	private Poll mPoll;
	private ArrayList<AnswerChoice> mItems = new ArrayList<AnswerChoice>();
	private Map<Integer, RadioButton> mRadioButtons = new HashMap<Integer, RadioButton>();

	public AnswerChoiceListAdapter(Context context, Poll poll, int viewMode) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
		this.mPoll = poll;
		this.mViewMode = viewMode;
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setPoll(Poll poll) {
		this.mPoll = poll;
	}

	public void addItems(List<AnswerChoice> listItems) {
		mItems.addAll(listItems);
	}

	public void addItem(AnswerChoice listItem) {
		mItems.add(listItem);
	}
	
	public void setViewMode(int viewMode) {
		mViewMode = viewMode;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item_answer_choice, null);

			holder.mLayoutAnswerChoiceText = (LinearLayout) convertView.findViewById(R.id.layout_answer_choice_text);
			holder.mLayoutAnswerChoiceRatingBar = (LinearLayout) convertView
					.findViewById(R.id.layout_answer_choice_ratingbar);

			holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
			holder.mRadioButton = (RadioButton) convertView.findViewById(R.id.radio_button);
			holder.mRadioButton.setOnClickListener(mRadioButtonClicklistener);
			holder.mSequence = (TextView) convertView.findViewById(R.id.sequence);
			
			holder.mAnswerChoiceName = (TextView) convertView.findViewById(R.id.answer_choice_name);
			holder.mAnswerChoiceRatingBar = (RatingBar) convertView.findViewById(R.id.answer_choice_ratingbar);
			holder.mVoterCount = (TextView) convertView.findViewById(R.id.voter_count);

			holder.mLayoutAnswerChoiceMedia = (LinearLayout) convertView.findViewById(R.id.layout_answer_choice_media);
			holder.mAnswerChoiceImage = (ImageView) convertView.findViewById(R.id.image_answer_choice);
			
			holder.mLayoutAnswerRate = (LinearLayout) convertView.findViewById(R.id.layout_answer_rate);
			holder.mProgressbarAnswerRate = (ProgressBar) convertView.findViewById(R.id.progressbar_answer_rate);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		AnswerChoice listItem = mItems.get(position);

		holder.mAnswerChoiceName.setText(listItem.getName());
		holder.mVoterCount.setText(String.valueOf(listItem.getVoterCount()));
		holder.mCheckBox.setId(position);
		holder.mRadioButton.setId(position);
		holder.mVoterCount.setId(position);
		holder.mSequence.setText(String.valueOf(listItem.getSequence()));
		
		mRadioButtons.put(position, holder.mRadioButton);

		Drawable image = listItem.getImage();
		if (image != null && (mPoll.getQuestionType().equals(Poll.MULTIPLE_CHOICE_TYPE) || mPoll.getQuestionType().equals(Poll.SINGLE_CHOICE_TYPE))) {
			holder.mLayoutAnswerChoiceMedia.setVisibility(View.VISIBLE);
			setAnswerChoiceImageBitmap(image, holder.mAnswerChoiceImage);
		} else {
			holder.mLayoutAnswerChoiceMedia.setVisibility(View.INVISIBLE);
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mAnswerChoiceImage.getLayoutParams();
			params.height = 0;
			holder.mAnswerChoiceImage.setLayoutParams(params);
			holder.mAnswerChoiceImage.setImageDrawable(null);
		}

		if (mViewMode == MODIFY_MODE) {
			if (mPoll.getQuestionType().equals(Poll.MULTIPLE_CHOICE_TYPE)) {
				holder.mCheckBox.setVisibility(View.VISIBLE);
				holder.mRadioButton.setVisibility(View.INVISIBLE);
				holder.mLayoutAnswerChoiceRatingBar.setVisibility(View.INVISIBLE);
				holder.mLayoutAnswerRate.setVisibility(View.INVISIBLE);

				ViewGroup.LayoutParams params = holder.mLayoutAnswerChoiceRatingBar.getLayoutParams();
				params.height = 0;
				
				params = holder.mLayoutAnswerRate.getLayoutParams();
				params.height = 0;
			} else if (mPoll.getQuestionType().equals(Poll.SINGLE_CHOICE_TYPE)) {
				holder.mCheckBox.setVisibility(View.INVISIBLE);
				holder.mRadioButton.setVisibility(View.VISIBLE);
				holder.mLayoutAnswerChoiceRatingBar.setVisibility(View.INVISIBLE);
				holder.mLayoutAnswerRate.setVisibility(View.INVISIBLE);
				
				ViewGroup.LayoutParams params = holder.mLayoutAnswerChoiceRatingBar.getLayoutParams();
				params.height = 0;
				
				params = holder.mLayoutAnswerRate.getLayoutParams();
				params.height = 0;
			} else if (mPoll.getQuestionType().equals(Poll.RATINGBAR_TYPE)) {
				holder.mLayoutAnswerChoiceRatingBar.setVisibility(View.VISIBLE);
				holder.mLayoutAnswerChoiceText.setVisibility(View.INVISIBLE);
				holder.mLayoutAnswerRate.setVisibility(View.INVISIBLE);
				
				ViewGroup.LayoutParams params = holder.mLayoutAnswerRate.getLayoutParams();
				params.height = 0;
				
			}			
		}
		else {
			holder.mCheckBox.setVisibility(View.INVISIBLE);
			holder.mRadioButton.setVisibility(View.INVISIBLE);
			holder.mLayoutAnswerChoiceRatingBar.setVisibility(View.INVISIBLE);
			
			ViewGroup.LayoutParams params = holder.mLayoutAnswerChoiceRatingBar.getLayoutParams();
			params.height = 0;
			
			holder.mSequence.setVisibility(View.VISIBLE);
			holder.mLayoutAnswerRate.setVisibility(View.VISIBLE);
			holder.mProgressbarAnswerRate.setVisibility(View.VISIBLE);
			
			params = holder.mLayoutAnswerRate.getLayoutParams();
			float progressbarHeight = AndroidUtil.convertDpToPixel(15, convertView.getContext());
			params.height = (int) progressbarHeight;
			
			double progress = listItem.getAnswerRate() * 100;
			holder.mProgressbarAnswerRate.setProgress((int) progress);
		}

		return convertView;
	}

	/**
	 * Resizes the image proportionately so it fits the entire space.
	 */
	@SuppressWarnings("deprecation")
	private void setAnswerChoiceImageBitmap(Drawable d, ImageView imageView) {
		float width = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();

		BitmapDrawable drawable = (BitmapDrawable) d;
		Bitmap bitmap = drawable.getBitmap();

		float i = ((float) width) / ((float) bitmap.getWidth());
		float imageHeight = i * (bitmap.getHeight());
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
		params.height = (int) imageHeight;
		params.width = (int) width;
		imageView.setLayoutParams(params);
		imageView.setImageDrawable(d);
	}
	
	OnClickListener mRadioButtonClicklistener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			RadioButton rb = (RadioButton) v;
			Log.d(TAG, "radio selected = " + rb.getId());

			for (Map.Entry<Integer, RadioButton> entry : mRadioButtons.entrySet()) {
				if (entry.getKey() !=  rb.getId()) {
					RadioButton other = (RadioButton) entry.getValue();
					other.setChecked(false);
				}
			}

			notifyDataSetChanged();
		}
	};
	
	/**
	 * View holder for a list item.
	 * 
	 */
	class ViewHolder {

		LinearLayout mLayoutAnswerChoiceText;
		LinearLayout mLayoutAnswerChoiceRatingBar;
		RatingBar mAnswerChoiceRatingBar;
		RadioButton mRadioButton;
		CheckBox mCheckBox;
		TextView mSequence;
		TextView mAnswerChoiceName;
		TextView mVoterCount;
		ImageView mAnswerChoiceImage;
		LinearLayout mLayoutAnswerChoiceMedia;
		LinearLayout mLayoutAnswerRate;
		ProgressBar mProgressbarAnswerRate;
	}
}

